package com.vcc.msc.sbs.entity;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlPullParserFactory;

public class VersionEntity extends Entity<VersionEntity> {
	private String typeCode;
	private String versionNo;
	private String description;

	@Override
	public String toString() {

		return "Type code : " + typeCode + ">>VersionNo : " + versionNo
				+ ">>Decs : " + description;
	}

	public String serialize() throws JSONException {
		return serializeToObj().toString();
	}

	public JSONObject serializeToObj() throws JSONException {
		JSONObject serializedObj = new JSONObject();
		serializedObj.put("typeCode", this.typeCode);
		serializedObj.put("versionNo", this.versionNo);
		serializedObj.put("description", this.description);

		return serializedObj;
	}

	public static List<VersionEntity> deserialize(String xml) {
		List<VersionEntity> versionList = new ArrayList<VersionEntity>();
		String tag = "";
		int parserEvent = -1;
		if (xml == null) {
			return null;
		}
		try {
			XmlPullParserFactory parserFactory = XmlPullParserFactory
					.newInstance();
			XmlPullParser parser = parserFactory.newPullParser();
			parser.setInput(new StringReader(xml));
			// calls service (referenced in url) to request XML serialized data
			parserEvent = parser.getEventType();

			VersionEntity version = null;
			while (parserEvent != XmlPullParser.END_DOCUMENT) {
				switch (parserEvent) {
				case XmlPullParser.START_TAG:
					tag = parser.getName();
					if (tag.compareTo("version") == 0) {
						version = new VersionEntity();
					}
					break;
				case XmlPullParser.END_TAG:
					tag = parser.getName();
					if (tag.compareTo("version") == 0) {
						versionList.add(version);
					}
					break;
				case XmlPullParser.TEXT:
					String text = parser.getText();
					if (text.trim().length() == 0)
						break;

					if (tag.compareTo("typeCode") == 0) {
						version.setTypeCode(text);
					} else if (tag.compareTo("versionNo") == 0) {
						version.setVersionNo(text);
					} else if (tag.compareTo("description") == 0) {
						version.setDescription(text);
					}

					break;
				}
				// versionList.add(version);
				parserEvent = parser.next();
			}
		} catch (XmlPullParserException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println(versionList);
		return versionList;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public String getTypeCode() {
		return typeCode;
	}

	public void setTypeCode(String typeCode) {
		this.typeCode = typeCode;
	}

	public String getVersionNo() {
		return versionNo;
	}

	public void setVersionNo(String versionNo) {
		this.versionNo = versionNo;
	}
}
